

#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;

class Solution
{
public:
    string decodeString(string s)
    {
        string st;

        for (int i = 0, n = s.size(); i < n; ++i)
        {
            if (s[i] != ']')
            {
                st += s[i];
            }
            else
            {
                string tmp;
                string scnt;
                string stmp;

                int j = st.size()-1;
                while (j >= 0 && st[j] != '[')
                {
                    stmp += st[j--];
                }
                reverse(stmp.begin(), stmp.end());

                --j;
                while (j >= 0 && isdigit(st[j]))
                {
                    scnt += st[j--];
                }
                reverse(scnt.begin(), scnt.end());

                for (int k = 0, n = std::stoi(scnt); k < n; ++k)
                {
                    tmp += stmp;
                }
                st.resize(j + 1);
                st += tmp;
            }
        }

        return st;
    }

    bool isAnagram(string s, string t)
    {
        if (s.size() != t.size())
            return false;

        vector<int> cnt(26);

        for (auto e : s)
        {
            ++cnt[e - 'a'];
        }
        for (auto e : t)
        {
            if (--cnt[e - 'a'] < 0)
                return false;
        }

        return true;
    }

    bool halvesAreAlike(string s)
    {
        int n = s.size();
        unordered_set<char> hash({ 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' });

        int cnt1 = 0, cnt2 = 0;
        for (int beg = 0, end = n - 1; beg < n / 2 && end >= n / 2; ++beg, --end)
        {
            if (hash.count(s[beg]))
                ++cnt1;
            if (hash.count(s[end]))
                ++cnt2;
        }

        return cnt1 == cnt2;
    }

    bool checkAlmostEquivalent(string word1, string word2)
    {
        vector<int> cnt1(26), cnt2(26);

        for (const auto& e : word1)
        {
            ++cnt1[e - 'a'];
        }
        for (const auto& e : word2)
        {
            ++cnt2[e - 'a'];
        }

        for (int i = 0; i < 26; ++i)
        {
            if (abs(cnt1[i] - cnt2[i]) > 3)
                return false;
        }
        return true;
    }
    
    string reverseWords(string s)
    {
        int beg = 0, end = 0;
        for (int n = s.size(); end < n; ++end)
        {
            if (isblank(s[end]))
            {
                reverse(s.begin() + beg, s.begin() + end);
                beg = end + 1;
            }
        }
        reverse(s.begin() + beg, s.begin() + end);

        return s;
    }

    int maxPower(string s)
    {
        int res = 0;

        for (int lf = 0, rg = 0, n = s.size(); rg < n;)
        {
            while (s[rg] == s[lf])
            {
                ++rg;
            }
            res = max(res, rg - lf);

            if (s[rg] != s[lf])
                lf = rg;
        }

        return res;
    }
};

void Test1()
{
    cout << Solution().decodeString("3[a]2[bc]") << endl;
    cout << Solution().decodeString("3[a2[c]]") << endl;

    const int a = 10;
    int* p = (int*)(&a);
    //int* p = &a;
    *p = 20;
    cout << "a = " << a << ", *p = " << *p << endl;
}

int main()
{
    Test1();

	return 0;
}